/********************************************************************************
* Description: epoll reactor
* Author: hsqiang
* Create Date: 2023-06-17
********************************************************************************/
#ifndef POLLER_HPP_
#define POLLER_HPP_

#include "channel.hpp"
#include "assert_define.hpp"
#include "noncopyable.hpp"
#include <vector>
#include <map>
#include <cstring>
#include <sys/epoll.h>
#include <unistd.h>

namespace event_framework
{
    using ChannelList = std::vector<ChannelPtr>;
    class Poller final : Noncopyable
    {
    public:
        Poller() = default;
        ~Poller()
        {
            ASSERT(epfd > 0);
            close(epfd);
            epfd = -1;
        }
        bool Init();
        bool UpdateChannel(ChannelPtr ch);
        void Poll(ChannelList& activeChannels);
    private:
        bool Update(int op, ChannelPtr channel);
        void FillActiveChannels(int num, ChannelList& list);
        
        int epfd { -1 };
        using EventList = std::vector<struct epoll_event>;
        EventList events;
        std::map<int, ChannelPtr> channelMap;
    };
}

#endif